#!/usr/bin/env python
__all__ = ( "loadUiType")

import compiler
from PyQt4 import QtCore

def loadUiType(uifile, from_imports=False):
	"""loadUiType(uifile) -> (form class, base class)

	Load a Qt Designer .ui file and return the generated form class and the Qt
	base class.

	uifile is a file name or file-like object containing the .ui file.
	from_imports is optionally set to use import statements that are relative
	to '.'.
	"""

	import sys

	from PyQt4 import QtGui

	if sys.hexversion >= 0x03000000:
		from PyQt4.uic.port_v3.string_io import StringIO
	else:
		from PyQt4.uic.port_v2.string_io import StringIO

	code_string = StringIO()
	comp=compiler.Compiler(uifile)
	try:
		winfo = comp.compileUi(uifile, code_string, from_imports )
	except:
		winfo = comp.compileUi(uifile, code_string)

	ui_globals = {}
	exec(code_string.getvalue(), ui_globals)

	return (ui_globals[winfo["uiclass"]], getattr(QtGui, winfo["baseclass"]))
